package com.atguigu.gmall.pay.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gmall.common.util.DateUtil;
import com.atguigu.gmall.feign.order.OrderFeignClient;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.pay.config.AlipayProperties;
import com.atguigu.gmall.pay.service.AlipayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-23 16:42
 **/
@Service
public class AlipayServiceImpl implements AlipayService {
    @Autowired
    AlipayClient alipayClient;
    @Autowired
    AlipayProperties alipayProperties;
    @Autowired
    OrderFeignClient orderFeignClient;

    @Override
    public String generatePayPage(Long orderId) throws AlipayApiException {
        // 1.获得初始化的AlipayClient: 通过AlipayAutoConfiguration得到
        // 2.设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(alipayProperties.getReturnUrl()); // 同步通知，跳转页面【浏览器跳转】
        alipayRequest.setNotifyUrl(alipayProperties.getNotifyUrl()); // 异步通知，后台悄悄发请求告诉我们哪个单支付成功了【异步通知】

        // 3.订单交易数据
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId).getData(); // 用户id拦截器处理了，隐式透传
        Date expireTime = orderInfo.getExpireTime(); //得到订单过期时间
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = orderInfo.getOutTradeNo();
        //付款金额，必填
        String total_amount = orderInfo.getTotalAmount().toString();
        //订单名称，必填
        String subject = "Mall订单-" + orderInfo.getOutTradeNo();
        //商品描述，可空
        String body = orderInfo.getTradeBody();

        // 4.设置业务参数
        Map<String, String> content = new HashMap<>();
        content.put("out_trade_no", out_trade_no);
        content.put("total_amount", total_amount);
        content.put("subject", subject);
        content.put("body", body);
        content.put("product_code", "FAST_INSTANT_TRADE_PAY");
        // 下单的时候告诉支付宝订单[绝对超时时间]；区别于后面再远程调用关单（远程调用不一定成功）【时间格式同支付宝文档规定的一样】
        content.put("time_expire", DateUtil.formatDate(expireTime, "yyyy-MM-dd HH:mm:ss"));
        alipayRequest.setBizContent(JSON.toJSONString(content));

        // 5.执行支付请求，得到响应；响应的是HTML代码段，浏览器一渲染就会自动提交，同时跳转到支付宝的二维码收银台界面
        String html = alipayClient.pageExecute(alipayRequest).getBody();

        return html;
    }

    @Override
    public boolean checkSign(Map<String, String> params) throws AlipayApiException {
        boolean check = AlipaySignature.rsaCheckV1(params,
                alipayProperties.getAlipayPublicKey(),
                alipayProperties.getCharset(),
                alipayProperties.getSignType());//调用SDK验证签名
        return check;
    }
}
